package com.nulldev.util.web.URLUtils;

import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.VariableAPI.util.strings.splitters.third_party.Splitter;

public class Entry {
	private static final Splitter PAT = Splitter.onPattern(": |=");
	private final String key, value;

	public Entry(final String key, final String value) {
		this.key = key;
		this.value = value;
	}

	public Entry(final String key, final Object value) {
		this.key = key;
		this.value = String.valueOf(value);
	}

	/**
	 * Creates an entry
	 * 
	 * @param string <br>
	 *               Accepted formats: <br>
	 *               - value=1 <br>
	 *               - value: 2 <br>
	 */
	public Entry(final String string) {
		final String[] split = PAT.splitToArray(string);
		this.key = split[0];
		this.value = ArrayUtils.StringArrayUtils.addFrom(split, 1);
	}

	public String getKey() {
		return this.key;
	}

	public String getValue() {
		return this.value;
	}

	@Override
	public String toString() {
		return this.key + ": " + this.value;
	}

	public boolean isValid() {
		return (this.key != null && this.value != null) && (!this.key.isEmpty() && !this.value.isEmpty());
	}
}
